#import <AVPlaySDK/AVPlaySDK.h>
#import <VCS_WS_ClientSDK/VCS_WS_ClientSDK.h>

#import "CDVVcsWsVideo.h"
#import "MonitorViewController.h"

@implementation CDVVcsWsVideo

/**
 *  初始化库(只初始化一次)
 */
- (void)_ready:(CDVInvokedUrlCommand*)command {
    CDVPluginResult* pluginResult = nil;

    printf("初始化播放库...");
    BOOL bSuccess = [AVPlaySDK AVPLAY_Startup];
    if (!bSuccess) {
        NSLog(@"init AVPlaySDK failed!!!");
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    }

    printf("初始化网络通讯库...");
    VCS_WS_AppInfo *appInfo = [[VCS_WS_AppInfo alloc] init];
    appInfo.app_id = @"test_user"; // 使用自身的appid，test_user仅供测试使用 //
    [[VCS_WS_ClientSDK defaultService] setAppInfo:appInfo];
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"OK"];

    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

/**
 *  播放视频
 */
-(void) playVideo:(CDVInvokedUrlCommand*)command {
    self.playVideoCallback = command.callbackId;

    NSDictionary*  options;
    if (command.arguments.count == 0) {
        options = [NSDictionary dictionary];
    } else {
        options = command.arguments[0];
    }

    NSLog(@"showFrameInfo is %@ ", options[@"showFrameInfo"]);
    NSLog(@"title is %@ ", options[@"title"]);
    NSLog(@"serverIP is %@ ", options[@"serverIP"]);
    NSLog(@"protocol is %@ ", options[@"protocol"]);
    NSLog(@"username is %@ ", options[@"username"]);
    NSLog(@"password is %@ ", options[@"password"]);
    NSLog(@"streamQuality is %zd ", [options[@"streamQuality"]intValue]);

    // VCS 格式视频参数
    NSLog(@"realPlay is %@ ", options[@"realPlay"]);
    NSLog(@"cameraUuid is %@ ", options[@"cameraUuid"]);
    NSLog(@"channelIndex is %zd ", [options[@"channelIndex"]intValue]);

    // WCS 格式视频参数
    NSLog(@"path is %@ ", options[@"path"]);
    NSLog(@"nodeUid is %@ ", options[@"nodeUid"]);

    // // JUST FOR TEST
    // options = @{@"url":@"ws://47.99.174.220:3098/vcs",
    //             @"username":@"test_user",
    //             @"serverIP":@"47.99.174.220",
    //             @"protocol":@"vcs",
    //             @"password":@"123456",
    //             @"realPlay":@"00E26C683636",
    //             @"cameraUuid":@"c8f125a9-391f-3f55-9823-d908d39d78f3",
    //             @"channelIndex":@"4",
    //             @"streamQuality":@"0"
    // };
    self.monitorViewController = [[MonitorViewController alloc] init];
    self.monitorViewController.options = options;
    self.monitorViewController.delegate = self;

    // [self.viewController addChildViewController:self.monitorViewController];
    // [[self.viewController navigationController] pushViewController:self.monitorViewController animated:YES];
    [self.viewController presentViewController:self.monitorViewController animated:YES completion:nil];
}

- (void)notifyWebView:(NSString *)msg {
    NSLog(@"---> %@", msg);
    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:msg];
    if (![msg isEqualToString:@"VIDEO_ERROR"]) {
        [pluginResult setKeepCallbackAsBool:YES];
    }
    [self.commandDelegate sendPluginResult:pluginResult callbackId:self.playVideoCallback];
}


@end
